package com.itms.job;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itms.entity.LocTypeRef;
import com.itms.enums.LocType;
import com.itms.enums.TableName;
import com.itms.mapper.LocMapper;
import com.itms.mapper.LocTypeRefMapper;
import com.itms.mapper.TableDataSyncMapper;
import com.itms.pojo.Loc;
import com.itms.pojo.TableDataSync;
import com.itms.service.BasicDataService;
import com.itms.service.LogService;
import com.itms.service.SycService;
import com.itms.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
@EnableScheduling
public class LocJob {

    @Autowired
    private LogService logService;

    @Reference
    private BasicDataService basicDataService;

    @Autowired
    private SycService sycService;

    @Autowired
    private LocMapper locMapper;

    @Autowired
    private LocTypeRefMapper locTypeRefMapper;

    @Scheduled(initialDelayString = "${LocJob.initialDelayString}",fixedDelayString="${LocJob.fixedDelayString}" )
    public void doSchedule(){
        List<Loc> errorList = new ArrayList<>();
        try {
            Date mdb = basicDataService.getTableSyncByTableName(TableName.MDBLoc.toString());
            Date itms = this.sycService.getMaxSyncDate(TableName.MDBLoc.toString());
            if (mdb==null) {
                return;
            };
            if (itms!=null && TimeUtils.getDateSequence(mdb).equals( TimeUtils.getDateSequence(itms))) {
                return;
            }
                List<Loc> list =this.basicDataService.getLocList(itms);
                for (Loc c:list) {

                    if(!"SGM".equals(c.getCustomerCode())){
                        continue;
                    }

                    com.itms.entity.Loc loc = this.locMapper.selectByLocCode(c.getLocCode(),null);
                    if (loc==null){
                        loc = new com.itms.entity.Loc();
                        setLoc(c, loc);
                        loc.setCreateDate(new Date());
                        loc.setCreateUserId(new BigDecimal(9999));
                        this.locMapper.insertSelective(loc);
                    }else {
                        setLoc(c, loc);
                        this.locMapper.updateByPrimaryKeySelective(loc);
                    }
                    String locType = c.getLocType();
                    if(locType == null) continue;
                    String[] types = locType.split(",");
                    for (int i = 0; i <types.length ; i++) {
                        LocTypeRef locTypeRef = this.locTypeRefMapper.selectByLocIdType(loc.getId(),types[i]);
                        if (locTypeRef == null){
                            locTypeRef = new LocTypeRef();
                            locTypeRef.setLocType(types[i]);
                          String s =  LocType.getTypeFromName(types[i]);
                            locTypeRef.setLocTypeCode(LocType.getTypeFromName(types[i]));
                            locTypeRef.setIsValid("1");
                            locTypeRef.setCreateUserId(new BigDecimal(9999));
                            locTypeRef.setCreateDate(new Date());
                            locTypeRef.setLastUpdateUserId(new BigDecimal(9999));
                            locTypeRef.setLastUpdateDate(new Date());
                            locTypeRef.setLocId(loc.getId());
                            this.locTypeRefMapper.insertSelective(locTypeRef);
                        }
                    }
                }
            if (errorList.size() == 0)
                this.sycService.save(mdb,TableName.MDBLoc.toString());
            this.logService.saveErrorList(new ArrayList(), TableName.MDBLoc.toString(), this.getClass().getName());

        }catch(Exception e){
            e.printStackTrace();
            this.logService.saveLog(e, TableName.MDBLoc.toString(),this.getClass().getName());
        }
    }

    private static void setLoc(Loc c, com.itms.entity.Loc loc) {
        loc.setIsValid(c.getIsValid());
        loc.setLocationCode(c.getLocCode());
        loc.setLocationName(c.getLocName());
        loc.setAddress(c.getAddress());
        loc.setContactname(c.getUserName());
        loc.setTel(c.getTel());
        loc.setProvince(c.getProvince());
        loc.setCity(c.getCity());
        loc.setDistrict(c.getDistrict());
        loc.setMobile(c.getMobile());
        loc.setLastUpdateDate(new Date());
        loc.setLastUpdateUserId(new BigDecimal(9999));
    }

}
